﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Weka
{
    //    internal abstract class ObjectDeserialiser
    //  {
    //    internal abstract void FromData(StreamReader streamReader);
    //}

    internal sealed class ObjectDeserialiser<T> //: ObjectDeserialiser
        where T : new()
    {
        internal FieldDeserialiser<T>[] FieldDeserialisers { get; set; }
        internal Action<T> MissingValueSetter { get; set; }

        internal T FromData(StreamReada streamReader)
        {
            T t = new T();

            var fieldDeserialisers = FieldDeserialisers;

            for (int i = 0; i < fieldDeserialisers.Length; i++)
                fieldDeserialisers[i].ReadHeader(streamReader);

            streamReader.HeaderClear();

            for (int i = 0; i < fieldDeserialisers.Length; i++)
                fieldDeserialisers[i].ReadWriteValue(t, streamReader);

            if (MissingValueSetter != null)
                MissingValueSetter(t);

            return t;
        }
    }
}
